package com.neuedu.oasystem.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.neuedu.oasystem.dao.IPrepareMeetingDao;
import com.neuedu.oasystem.entity.PrepareMeeting;
import com.neuedu.oasystem.util.DBHelper;

public class PrepareMeetingDaoImpl extends BaseDao implements IPrepareMeetingDao {

	@Override
	public boolean add(PrepareMeeting param) {
		Connection connection = null;
		PreparedStatement statement = null;
		connection = getConnection();
		try {
			String sql = "insert into Prepare_Meeting(Meeting_Id,speaker,meeting_format,meeting_Theme,start_Time,end_Time,meeting_Place,Meeting_Content)"
					+ "	values(?,?,?,?,?,?,?,?)";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, param.getId());
			statement.setString(2, param.getSpeaker());
			statement.setString(3, param.getMeetingForma());
			statement.setString(4, param.getMeetingTheme());
			statement.setTimestamp(5, new java.sql.Timestamp(param.getStartTime().getTime()));
			statement.setTimestamp(6, new java.sql.Timestamp(param.getEndTime().getTime()));
			statement.setString(7, param.getMeetingPlace());
			statement.setString(8, param.getMeetingContent());
			int count = statement.executeUpdate();
			if (count == 1) {
				return true;
			}
		} catch (SQLException e) {
			//throw new RuntimeException(e.getMessage());
		} finally {
			DBHelper.closeAll(null, statement, null);
		}

		return false;
	}

	@Override
	public boolean edit(PrepareMeeting param) {
		Connection connection = null;
		PreparedStatement statement = null;
		connection = getConnection();
		int count = 0;
		try {
			String sql = "update Prepare_Meeting set speaker=?,meeting_format=?,meeting_Theme=?,"
					+ "start_Time=?,end_Time=?,meeting_Place=?,Meeting_Content=? where Meeting_Id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(8, param.getId());
			statement.setString(1, param.getSpeaker());
			statement.setString(2, param.getMeetingForma());
			statement.setString(3, param.getMeetingTheme());
			statement.setTimestamp(4, new java.sql.Timestamp(param.getStartTime().getTime()));
			statement.setTimestamp(5, new java.sql.Timestamp(param.getEndTime().getTime()));
			statement.setString(6, param.getMeetingPlace());
			statement.setString(7, param.getMeetingContent());
			count = statement.executeUpdate();
			if (count == 1) {
				return true;
			}
		} catch (SQLException e) {
			//throw new RuntimeException(e.getMessage());
		} finally {
			DBHelper.closeAll(null, statement, null);
		}

		return false;
	}
	

	@Override
	public boolean deleteById(int id) {
		Connection connection = null;
		PreparedStatement statement = null;
		connection = getConnection();
		try {
			String sql = "delete from Prepare_Meeting where Meeting_Id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			int count = statement.executeUpdate();
			if (count == 1) {
				return true;
			}
		} catch (SQLException e) {
			//throw new RuntimeException(e.getMessage());
		} finally {
			DBHelper.closeAll(null, statement, null);
		}
		return false;
	}

	@Override
	public PrepareMeeting findById(int id) {

		return null;
	}
	

	@Override
	public List<PrepareMeeting> findAll() {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		PrepareMeeting prepareMeeting;
		List<PrepareMeeting> list = new ArrayList<PrepareMeeting>();
		connection = getConnection();
		try {
			String sql = "select MEETING_ID,speaker,MEETING_FORMAT,meeting_theme,"
					+ "start_Time,end_Time,meeting_Place,Meeting_Content from Prepare_Meeting";
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int meetingId = resultSet.getInt("Meeting_Id");
				String speaker = resultSet.getString("speaker");
				String meetingForma = resultSet.getString("MEETING_FORMAT");
				String meetingTheme = resultSet.getString("meeting_Theme");
				Date startTime = resultSet.getTimestamp("start_Time");
				Date endTime = resultSet.getTimestamp("end_Time");
				String meetingPlace = resultSet.getString("meeting_Place");
				String meetingContent = resultSet.getString("Meeting_Content");
				prepareMeeting = new PrepareMeeting(meetingId, speaker, meetingForma, meetingTheme,
						startTime, endTime, meetingPlace, meetingContent);
				list.add(prepareMeeting);
			}
		} catch (SQLException e) {
			//throw new RuntimeException(e.getMessage());
		} finally {
			DBHelper.closeAll(resultSet, statement, null);
		}

		return list;
	}

	@Override
	public List<PrepareMeeting> findByExample(PrepareMeeting param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PrepareMeeting> findByPager(int page, int size) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int addAll(List<PrepareMeeting> useparamrs) {
		int result = 0;
		Connection connection = null;
		PreparedStatement statement = null;
		connection = getConnection();
		try {
			String sql = "insert into Prepare_Meeting(Meeting_Id,speaker,meeting_format,meeting_Theme,start_Time,end_Time,meeting_Place,Meeting_Content)"
					+ "	values(?,?,?,?,?,?,?,?)";
			statement = connection.prepareStatement(sql);
			for (PrepareMeeting prepareMeeting : useparamrs) {
				statement.setInt(1, prepareMeeting.getId());
				statement.setString(2, prepareMeeting.getSpeaker());
				statement.setString(3, prepareMeeting.getMeetingForma());
				statement.setString(4, prepareMeeting.getMeetingTheme());
				statement.setTimestamp(5, new java.sql.Timestamp(prepareMeeting.getStartTime().getTime()));
				statement.setTimestamp(6, new java.sql.Timestamp(prepareMeeting.getEndTime().getTime()));
				statement.setString(7, prepareMeeting.getMeetingPlace());
				statement.setString(8, prepareMeeting.getMeetingContent());
				statement.addBatch();
			}
			int[] count = statement.executeBatch();
			for (int i : count) {
				if (i==-2) {
					result++;
				}
			}
			if (result==count.length) {
				return result;
			}
		} catch (SQLException e) {
			//throw new RuntimeException(e.getMessage());
		}finally {
			DBHelper.closeAll(null, statement, null);
		}
		
		
		return 0;
	}

	@Override
	public int editAll(List<PrepareMeeting> param) {
		// TODO Auto-generated method stub
		return 0;
	}



}
